
package com.zcp.wizard;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.util.Log;

public class BluetoothUtils {

    /**
     * 切换蓝牙开关状态
     * 
     * @param context
     */
    public static boolean toggleBluetooth(Context context) {
        boolean success = false;
        try {

            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if (adapter == null) {
                return false;
            }
            if (!adapter.isEnabled()) {
                success=adapter.enable();
                Log.i("tag1", "toggleBluetooth open "+success);
            } else {
                success=adapter.disable();
                Log.i("tag1", "toggleBluetooth close "+success);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;

    }

    /**
     * 获取蓝牙状态，true为开启，false为关闭
     * 
     * @return
     */
    public static boolean isEnabled() {
        boolean success = false;
        try {
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            // 表示没有蓝牙设备
            if (adapter == null) {
                return false;
            }
            success = adapter.isEnabled();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }

    public static int getState() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter == null) {
            return BluetoothAdapter.STATE_OFF;
        }
        return adapter.getState();
    }

}
